#include "additem.h"
#include "ui_additem.h"
#include <QPushButton>
#include <QDebug>
#include <QDateTime>
#include <QtSql/QSqlQueryModel>
#include <QtSql/QSqlQuery>
#include <QMouseEvent>

addItem::addItem(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::addItem)
{
    ui->setupUi(this);
    this->setWindowTitle("添加记账");

    //设置tab颜色
    this->setStyleSheet("QTabBar::tab{background-color:rgb(25,64,129);color:white;"
                        "width:200px;height:36px;}"
                        "QTabBar::tab::!selected{background-color:rgb(126,156,208);}");
    // 控件样式表
    ui->moneyEdit->setStyleSheet("QLineEdit{"
                                 //边界2像素 实线 颜色rgb  或者border:none 没有边界
                                 "border:2px solid rgb(255, 255, 255);"
                                 //背景的颜色
                                 "background: transparent;"
                                 //边角4像素圆滑
                                 "border-radius: 4px;"
                                 //选中时的背景颜色
                                 "selection-background-color: rgb(89,99,99);"
                                 "color:white;}"
                                 );

    ui->moneyEdit_2->setStyleSheet("QLineEdit{"
                                   //边界2像素 实线 颜色rgb  或者border:none 没有边界
                                   "border:2px solid rgb(255, 255, 255);"
                                   //背景的颜色
                                   "background: transparent;"
                                   //边角4像素圆滑
                                   "border-radius: 4px;"
                                   //选中时的背景颜色
                                   "selection-background-color: rgb(89,99,99);"
                                   "color:white;}"
                                   );

    ui->otherEdit->setStyleSheet("QTextEdit{"
                                 "border:2px solid rgb(255, 255, 255);"
                                 "background: transparent;"
                                 "border-radius: 4px;"
                                 "selection-background-color: rgb(89,99,99);"
                                 "color:white;}");

    ui->otherEdit_2->setStyleSheet("QTextEdit{"
                                   "border:2px solid rgb(255, 255, 255);"
                                   "background: transparent;"
                                   "border-radius: 4px;"
                                   "selection-background-color: rgb(89,99,99);"
                                   "color:white;}"
                                   );

    ui->outMoney->setStyleSheet("QComboBox{"
                                "border:2px solid rgb(255, 255, 255);"
                                "background: transparent;"
                                "border-radius: 4px;"
                                "color:white;}"
                                "QComboBox QAbstractItemView {background:rgb(31,73,145);"
                                "color:white;}");

    ui->inMoney->setStyleSheet("QComboBox{"
                                  "border:2px solid rgb(255, 255, 255);"
                                  "background: transparent;"
                                  "border-radius: 4px;"
                                  "color:white;}"
                                  "QComboBox QAbstractItemView {background:rgb(31,73,145);"
                                  "color:white;}");

    ui->dateTimeEdit->setStyleSheet("QDateTimeEdit{"
                                    "border:2px solid rgb(255, 255, 255);"
                                    "background: transparent;"
                                    "border-radius: 4px;"
                                    "selection-background-color: rgb(89,99,99);"
                                    "color:white;}"
                                    );

    ui->dateTimeEdit_2->setStyleSheet("QDateTimeEdit{"
                                      "border:2px solid rgb(255, 255, 255);"
                                      "background: transparent;"
                                      "border-radius: 4px;"
                                      "selection-background-color: rgb(89,99,99);"
                                      "color:white;}"
                                      );
    ui->money->setStyleSheet("QLabel{color:white;}");
    ui->money_2->setStyleSheet("QLabel{color:white;}");
    ui->theme->setStyleSheet("QLabel{color:white;}");
    ui->theme_2->setStyleSheet("QLabel{color:white;}");
    ui->date->setStyleSheet("QLabel{color:white;}");
    ui->date_2->setStyleSheet("QLabel{color:white;}");
    ui->mood->setStyleSheet("QLabel{color:white;}");
    ui->mood_2->setStyleSheet("QLabel{color:white;}");
    ui->other->setStyleSheet("QLabel{color:white;}");
    ui->other_2->setStyleSheet("QLabel{color:white;}");

    ui->save_btn->setStyleSheet("QPushButton{"
                                "border:2px solid rgb(255, 255, 255);"
                                "background: transparent;"
                                "border-radius: 4px;"
                                "color:white;}"
                                "QPushButton:hover{"
                                "background-color: rgb(89,99,99);"
                                "color:white;}"
                                );
    ui->save_btn_2->setStyleSheet("QPushButton{"
                                "border:2px solid rgb(255, 255, 255);"
                                "background: transparent;"
                                "border-radius: 4px;"
                                "color:white;}"
                                "QPushButton:hover{"
                                "background-color: rgb(89,99,99);"
                                "color:white;}"
                                );
    ui->back_btn->setStyleSheet("QPushButton{"
                                "border:2px solid rgb(255, 255, 255);"
                                "background: transparent;"
                                "border-radius: 4px;"
                                "color:white;}"
                                "QPushButton:hover{"
                                "background-color: rgb(89,99,99);"
                                "color:white;}"
                                );
    ui->back_btn_2->setStyleSheet("QPushButton{"
                                "border:2px solid rgb(255, 255, 255);"
                                "background: transparent;"
                                "border-radius: 4px;"
                                "color:white;}"
                                "QPushButton:hover{"
                                "background-color: rgb(89,99,99);"
                                "color:white;}"
                                );

    //设置默认定位
    ui->tabWidget->setCurrentIndex(0);

    //设置背景
    ui->outMoney->setAutoFillBackground(true);
    QPalette bgpal1 = ui->outMoney->palette();
    bgpal1.setBrush(QPalette::Window,QBrush(QPixmap(":/imageUsed/additemBG.png")
                           .scaled( ui->tabWidget->size(),Qt::KeepAspectRatio,Qt::SmoothTransformation)));
    ui->outMoney->setPalette(bgpal1);

    ui->inMoney->setAutoFillBackground(true);
    QPalette bgpal2 = ui->inMoney->palette();
    bgpal2.setBrush(QPalette::Window,QBrush(QPixmap(":/imageUsed/additemBG.png")
                           .scaled( ui->tabWidget->size(),Qt::KeepAspectRatio,Qt::SmoothTransformation)));
    ui->inMoney->setPalette(bgpal2);

    //限制文本编辑框接收为浮点型
    //限制输入范围0~999999999.99   限制小数点后两位
    QDoubleValidator *aDoubleValidator = new QDoubleValidator(0,999999999.99,2,this);
    ui->moneyEdit->setValidator(aDoubleValidator);
    ui->moneyEdit_2->setValidator(aDoubleValidator);

    //设置日期时间编辑框默认为系统时间
    ui->dateTimeEdit->setDateTime(QDateTime::currentDateTime());
    ui->dateTimeEdit_2->setDateTime(QDateTime::currentDateTime());

    //设置下拉栏的内容与默认显示
    QStringList list1,list2,list3;
    list1 << "食品酒水" << "衣服饰品" << "交流通讯" << "行车交通"
          << "寝室费用" << "学习进修" << "休闲娱乐" << "医疗保健" << "其他";
    list2 << "生活费" <<"工资" << "红包" << "兼职" << "奖金" << "其他";
    list3 << "开心" << "悲伤" << "愤怒" << "害怕";
    ui->themeBox->addItems(list1);
    ui->themeBox_2->addItems(list2);
    ui->themeBox->setCurrentText("食品酒水");
    ui->themeBox_2->setCurrentText("生活费");
    ui->moodBox->addItems(list3);
    ui->moodBox_2->addItems(list3);
    ui->moodBox->setCurrentText("开心");
    ui->moodBox_2->setCurrentText("开心");

    //save按钮
    connect(ui->save_btn,&QPushButton::clicked,[=](){
        addCash_out();
        this->close();
        emit windowClose();
    });
    connect(ui->save_btn_2,&QPushButton::clicked,[=](){
        addCash_in();
        this->close();
        emit windowClose();
    });
    ui->save_btn->setShortcut(Qt::Key_Return);
    ui->save_btn_2->setShortcut(Qt::Key_Return);
    //back按钮
    connect(ui->back_btn,&QPushButton::clicked,[=](){
        this->close();
        emit windowClose();
    });
    connect(ui->back_btn_2,&QPushButton::clicked,[=](){
        this->close();
        emit windowClose();
    });
}

void addItem::setUser(QString user)
{
    username=user;
    qDebug()<<username<<"表已打开，可添加数据";
}

//支出账单增加
void addItem::addCash_out()
{
    QString sql;
    QSqlQuery query;
    //接收各部分内容
    double money = ui->moneyEdit->text().toDouble();
    QString strTheme = ui->themeBox->currentText();
    int year = ui->dateTimeEdit->sectionText(QDateTimeEdit::YearSection).toInt();
    int month = ui->dateTimeEdit->sectionText(QDateTimeEdit::MonthSection).toInt();
    int day = ui->dateTimeEdit->sectionText(QDateTimeEdit::DaySection).toInt();
    int hour = ui->dateTimeEdit->sectionText(QDateTimeEdit::HourSection).toInt();
    int minute = ui->dateTimeEdit->sectionText(QDateTimeEdit::MinuteSection).toInt();
    QString strMood = ui->moodBox->currentText();
    QString strOther = ui->otherEdit->toPlainText();
    sql = QString("insert into %1(in_out, money, theme, date_year, date_month,"
                  " date_day, date_hour, date_minute, mood, other) values('支出','%2','%3','%4','%5','%6',"
                  "'%7','%8','%9','%10')").arg(username).arg(money).arg(strTheme).arg(year).arg(month).arg(day)
            .arg(hour).arg(minute).arg(strMood).arg(strOther);
    qDebug()<<sql;
    query.exec(sql);
}

//收入账单增加
void addItem::addCash_in()
{
    QString sql;
    QSqlQuery query;
    //接收各部分内容
    double money = ui->moneyEdit_2->text().toDouble();
    QString strTheme = ui->themeBox_2->currentText();
    int year = ui->dateTimeEdit_2->sectionText(QDateTimeEdit::YearSection).toInt();
    int month = ui->dateTimeEdit_2->sectionText(QDateTimeEdit::MonthSection).toInt();
    int day = ui->dateTimeEdit_2->sectionText(QDateTimeEdit::DaySection).toInt();
    int hour = ui->dateTimeEdit_2->sectionText(QDateTimeEdit::HourSection).toInt();
    int minute = ui->dateTimeEdit_2->sectionText(QDateTimeEdit::MinuteSection).toInt();
    QString strMood = ui->moodBox_2->currentText();
    QString strOther = ui->otherEdit_2->toPlainText();
    sql = QString("insert into %1(in_out, money, theme, date_year, date_month,"
                  " date_day, date_hour, date_minute, mood, other) values('收入','%2','%3','%4','%5','%6',"
                  "'%7','%8','%9','%10')").arg(username).arg(money).arg(strTheme).arg(year).arg(month).arg(day)
            .arg(hour).arg(minute).arg(strMood).arg(strOther);
    qDebug()<<sql;
    query.exec(sql);
}

addItem::~addItem()
{
    delete ui;
}
